'''
概念：
    元素定位/对象识别
八大定位方式：
    id      首选
    name    次选
    class   适合单class属性值的元素
    tag     用的很少很少
    xpath   很灵活、用的很多
    css     很灵活、用的很多
    link    适合文本链接
    partial_link    适合长的文本链接
'''

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from options import options

url = r'https://www.baidu.com/'
driver = webdriver.Chrome(options=options)
driver.get(url)
sleep(3)

# # 1、id定位：首选
# driver.find_element(By.ID, 'kw').send_keys('selenium')
# driver.find_element(By.ID, 'su').click()
# sleep(3)

# # 2、name定位：次选
# driver.find_element(By.NAME, 'wd').send_keys('selenium2')
# driver.find_element(By.ID, 'su').click()
# sleep(3)

# # 3、tag定位：了解
# driver.find_elements(By.TAG_NAME, 'input')[7].send_keys('selenium3')
# driver.find_elements(By.TAG_NAME, 'input')[8].click()
# sleep(3)

# # 4、class定位：适合定位 class 为单属性的元素
# driver.find_element(By.CLASS_NAME, 's_ipt').send_keys('s4')
# # driver.find_element(By.CLASS_NAME,'self-btn').click()
# driver.find_elements(By.CLASS_NAME, 'bg')[2].click()
# sleep(3)

# 5、xpath定位
'''
xpath基本语法
/       根路径
//      相对路径
*       所有标签
input   指定标签
[@id="kw"] 指定属性

'''
# # 5-1、绝对路径
# # /html/body/div[2]/div[1]/div[5]/div/div/form/span[1]/input
# # /html/body/div[2]/div[1]/div[5]/div/div/form/span[2]/input
# driver.find_element(By.XPATH,'/html/body/div[2]/div[1]/div[5]/div/div/form/span[1]/input').send_keys('s5')
# driver.find_element(By.XPATH,'/html/body/div[2]/div[1]/div[5]/div/div/form/span[2]/input').click()
# sleep(3)

# # 5-2、相對路径
# # //*[@id="kw"]
# # //*[@id="su"]
# driver.find_element(By.XPATH,'//*[@id="kw"]').send_keys('s6')
# driver.find_element(By.XPATH,'//*[@id="su"]').click()
# sleep(3)
#
# # 5-2、相對路径之指定标签
# driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('s7')
# driver.find_element(By.XPATH,'//input[@id="su"]').click()
# sleep(3)
#
# # 5-2、相對路径之指定上层元素的路径
# driver.find_element(By.XPATH,'//form[@id="form"]/span/input[@id="kw"]').send_keys('s8')
# driver.find_element(By.XPATH,'//input[@id="su"]').click()
# sleep(3)
#
# # 5-2、相對路径之指定多个属性（属性的逻辑运算）
# driver.find_element(By.XPATH,'//*[@id="kw" and @name="wd"]').send_keys('s9')
# driver.find_element(By.XPATH,'//*[@id="su"]').click()
# sleep(3)

# # 6、css定位
# '''
# #kw
# #su
# '''
# # css定位 之 id选择器
# driver.find_element(By.CSS_SELECTOR, '#kw').send_keys('s10')
# driver.find_element(By.CSS_SELECTOR, '#su').click()
# sleep(3)
#
# # css定位 之 class选择器
# driver.find_element(By.CSS_SELECTOR, '.s_ipt').send_keys('s11')
# driver.find_element(By.CSS_SELECTOR, '.bg .s_btn').click()  # 特别注意
# sleep(3)
#
# # css定位 之 使用其他属性
# driver.find_element(By.CSS_SELECTOR, '[name="wd"]').send_keys('s12')
# driver.find_element(By.CSS_SELECTOR, '.bg .s_btn').click()
# sleep(3)
#
# # css定位 之 使用多个属性
# driver.find_element(By.CSS_SELECTOR, '[name="wd"]').send_keys('s13')
# driver.find_element(By.CSS_SELECTOR, '.bg .s_btn[type="submit"][value="百度一下"]').click()
# sleep(3)
#
# # css定位 之 使用层级
# driver.find_element(By.CSS_SELECTOR, 'form[id="form"]>span>input[name="wd"]').send_keys('s14')
# driver.find_element(By.CSS_SELECTOR, '.bg .s_btn[type="submit"][value="百度一下"]').click()
# sleep(3)

# # 7、link定位
# driver.find_element(By.LINK_TEXT,'新闻').click()
# sleep(3)

# 8、partial_link定位
driver.find_element(By.PARTIAL_LINK_TEXT, '新').click()
sleep(3)

# driver.quit()
